package com.haixiaoke.saas.workOrder.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.common.constant.WorkOrderConstants;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.workOrder.domain.WorkOrder;
import com.haixiaoke.saas.workOrder.domain.WorkOrderEvaluate;
import com.haixiaoke.saas.workOrder.domain.WorkOrderRecord;
import com.haixiaoke.saas.workOrder.mapper.WorkOrderEvaluateMapper;
import com.haixiaoke.saas.workOrder.service.IWorkOrderEvaluateService;
import com.haixiaoke.saas.workOrder.service.IWorkOrderRecordService;
import com.haixiaoke.saas.workOrder.service.IWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 工单评价Service业务层处理
 *
 * @author qixi
 * @date 2024-09-04
 */
@Service
public class WorkOrderEvaluateServiceImpl implements IWorkOrderEvaluateService {
    @Autowired
    private WorkOrderEvaluateMapper workOrderEvaluateMapper;
    @Autowired
    private IWorkOrderService workOrderService;
    @Autowired
    private IWorkOrderRecordService workOrderRecordService;

    /**
     * 查询工单评价
     *
     * @param evaluateId 工单评价主键
     * @return 工单评价
     */
    @Override
    public WorkOrderEvaluate selectWorkOrderEvaluateByEvaluateId(Long evaluateId) {
        return workOrderEvaluateMapper.selectWorkOrderEvaluateByEvaluateId(evaluateId);
    }

    /**
     * 查询工单评价列表
     *
     * @param workOrderEvaluate 工单评价
     * @return 工单评价
     */
    @Override
    public List<WorkOrderEvaluate> selectWorkOrderEvaluateList(WorkOrderEvaluate workOrderEvaluate) {
        return workOrderEvaluateMapper.selectWorkOrderEvaluateList(workOrderEvaluate);
    }

    /**
     * 新增工单评价
     *
     * @param workOrderEvaluate 工单评价
     * @return 结果
     */
    @Override
    public int insertWorkOrderEvaluate(WorkOrderEvaluate workOrderEvaluate) {
        workOrderEvaluate.setCreateTime(DateUtils.getNowDate());
        return workOrderEvaluateMapper.insertWorkOrderEvaluate(workOrderEvaluate);
    }

    /**
     * 修改工单评价
     *
     * @param workOrderEvaluate 工单评价
     * @return 结果
     */
    @Override
    public int updateWorkOrderEvaluate(WorkOrderEvaluate workOrderEvaluate) {
        return workOrderEvaluateMapper.updateWorkOrderEvaluate(workOrderEvaluate);
    }

    /**
     * 批量删除工单评价
     *
     * @param evaluateIds 需要删除的工单评价主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderEvaluateByEvaluateIds(Long[] evaluateIds) {
        return workOrderEvaluateMapper.deleteWorkOrderEvaluateByEvaluateIds(evaluateIds);
    }

    /**
     * 删除工单评价信息
     *
     * @param evaluateId 工单评价主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderEvaluateByEvaluateId(Long evaluateId) {
        return workOrderEvaluateMapper.deleteWorkOrderEvaluateByEvaluateId(evaluateId);
    }

    @Override
    @Transactional
    public int evaluateWorkOrder(WorkOrderEvaluate workOrderEvaluate) {
        String workOrderId = workOrderEvaluate.getWorkOrderId();
        WorkOrder workOrder = workOrderService.selectWorkOrderByWorkOrderId(workOrderId);
        if (ObjectUtil.isNull(workOrder)) {
            throw new RuntimeException("工单不存在");
        }

        if (!WorkOrderConstants.WORK_ORDER_STATE_COMPLETED.equals(workOrder.getWorkOrderState()) ){
            throw new RuntimeException("当前工单状态不允许评价");
        }

        String estimationFlag = workOrder.getEstimationFlag();
        if (WorkOrderConstants.ESTIMATION_FLAG_YES.equals(estimationFlag)) {
            throw new RuntimeException("工单已评价");
        }

        WorkOrderRecord workOrderRecord = new WorkOrderRecord(workOrderId, WorkOrderConstants.RECORD_TYPE_USER_VISIBLE, WorkOrderConstants.DISPLAY_TYPE_ORDER_EVALUATED,
                workOrderEvaluate.getCreateBy() + "已评价工单", null, null, null);
        workOrderRecordService.insertWorkOrderRecord(workOrderRecord);


        workOrder.setEstimationFlag(WorkOrderConstants.ESTIMATION_FLAG_YES);
        workOrderService.updateWorkOrder(workOrder);


        workOrderEvaluate.setCreateTime(DateUtils.getNowDate());
        return workOrderEvaluateMapper.insertWorkOrderEvaluate(workOrderEvaluate);
    }

    @Override
    public List<WorkOrderEvaluate> selectWorkOrderEvaluateByWorkOrderIds(List<String> orderIds) {
        return workOrderEvaluateMapper.selectWorkOrderEvaluateByWorkOrderIds(orderIds);
    }

    @Override
    public WorkOrderEvaluate selectWorkOrderEvaluateByWorkOrderId(String workOrderId) {
        return workOrderEvaluateMapper.selectWorkOrderEvaluateByWorkOrderId(workOrderId);
    }
}
